﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GoodsInvoicing.Views.View;
using GoodsInvoicing.Presentions.PresenterImpl;
using GoodsInvoicing.Presentions.Presenter;
using Spring.Context.Support;
using Spring.Context;
using GoodsInvoicing.BLL.Service;
using GoodsInvoicing.Model.User;

namespace GoodsInvoicing.Views.ViewImpl
{
    public partial class LoginForm : Form, ILoginForm
    {
        private ILoginFormPresenter presenter;
        private string loginName = "", loginPWD = "";
        private bool isLogin = false;

        public LoginForm()
        {
            InitializeComponent();

            //初始化绑定
            IApplicationContext applicationContext = ContextRegistry.GetContext();
            presenter = new LoginFormPresenter(this, applicationContext["GI_UserService"] as IGI_UserService);
            presenter.InitializeView();
        }

        #region ILoginForm 成员
        public event EventHandler<EventArgs> LoginButtonClick;
        
        public string LoginName
        {
            get
            {
                return loginName;
            }
            set
            {
                this.loginName = value;
            }
        }

        public string LoginPWD
        {
            get
            {
                return loginPWD;
            }
            set
            {
                this.loginPWD = value;
            }
        }

        public bool IsLogin
        {
            get
            {
                return isLogin;
            }
            set
            {
                this.isLogin = value;
            }
        }
        #endregion

        /// <summary>
        /// Form验证
        /// </summary>
        private bool ValidateForm() 
        {
            epLogin.Clear();
            if (this.tbLoginUserName.Text.Length == 0)
            {
                epLogin.SetError(this.tbLoginUserName, "用户名不能为空！");
                return false;
            }
            if (this.tbLoginPWD.Text.Length == 0)
            {
                epLogin.SetError(this.tbLoginPWD, "密码不能为空！");
                return false;
            }
            return true;
        }

        /// <summary>
        /// 登录按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginButton_Click(object sender, EventArgs e)
        {
            if (ValidateForm())
            {
                this.LoginName = this.tbLoginUserName.Text.Trim();
                this.LoginPWD = this.tbLoginPWD.Text.Trim();

                if (this.LoginButtonClick != null)
                {
                    this.LoginButtonClick((sender as Button).Text, e);               
                }

                LoginFail();
            }               
        }

        /// <summary>
        /// 登录失败，用户名或密码错误
        /// </summary>
        private void LoginFail()
        {
            if (!this.IsLogin)
            {
                epLogin.Clear();
                epLogin.SetError(this.tbLoginUserName, "用户名密码不正确，请重新录入");
                epLogin.SetError(this.tbLoginPWD, "用户名密码不正确，请重新录入");
            }
        }

        private void LogoutButton_Click(object sender, EventArgs e) 
        {
            Application.Exit();
        }        

        public void LoginFormClose()
        {
            this.Hide();
        }

        public void MainFormShow(UserDetail user, MainForm form)
        {
            MainForm mainForm = new MainForm();
            mainForm.User = user;
            mainForm.Form = mainForm;
            mainForm.IsMdiContainer = true;
            mainForm.WindowState = FormWindowState.Maximized;
            mainForm.Show();
        }
    }
}
